/**
* @file sm3.h
 * @brief This file contains the definitions and function declarations for the SM3 hash algorithm.
 *
 * Function Declarations:
 *  - sm3_compress(): Compresses the message blocks using the SM3 compression function.
 *  - sm3_init(): Initializes the SM3 hash context structure.
 *  - sm3_update(): Updates the SM3 hash context with input data.
 *  - sm3_finish(): Completes the SM3 hash computation and generates the final hash value.
 *  - sm3(): Computes the SM3 digest of a given message.
 */

#ifndef SM_SM3_H
#define SM_SM3_H

#include "common.h"

/**
 * @struct sm3_ctx
 * @brief SM3 hash context structure. It contains the following members:
 * @var reg Array of 8 32-bit registers used in the SM3 algorithm.
 * @var msg Array of 64 bytes to hold the message being processed.
 * @var len 64-bit integer representing the length of the message in bytes.
 */
 
typedef struct SM3Context{
    u32 reg[8];
    u8  msg[64];
    u64 len;
} sm3_ctx;

typedef u8 sm3_digest[32];
/**
 * @brief Compresses the message blocks using the SM3 compression function.
 * @param msg A pointer to the message block that is to be compressed.
 * @param buf The current hash state.
 * @param num The number of message blocks to be processed.

 */
void sm3_compress(const u8 *msg, u32 buf[8], u32 num);

/**
 * @brief Initializes the SM3 hash context structure.
 * This function initializes various members of the SM3 context structure to default values, preparing for subsequent hash calculations.
 * @param ctx Pointer to the SM3 context structure.
*/
void sm3_init(sm3_ctx *ctx);

/**
 * @brief Updates the SM3 hash context with input data.
 * This function appends the provided input data to the current hash context and updates the hash state.
 * @param ctx  Pointer to the SM3 context structure.
 * @param data Pointer to the input data to be processed.
 * @param dlen Length of the input data, measured in bytes.
 */
void sm3_update(sm3_ctx *ctx, const void *data, u64 dlen);

/**
 * @brief Completes the SM3 hash computation and generates the final hash value.
 * This function processes the remaining data in the hash context and stores the computed hash value into the provided output buffer.
 * @param ctx  Pointer to the SM3 context structure.
 * @param dgst Output buffer for storing the computed hash value. Should be at least 32 bytes to accommodate the SM3 hash value.
 */
void sm3_finish(sm3_ctx *ctx, u8 dgst[32]);


/**
 * @brief Computes the SM3 digest of a given message.
 * This function performs the entire SM3 hashing process and stores the final digest.
 * @param digest The output buffer where the computed hash will be stored.
 * @param msg    A pointer to the input message that is to be hashed.
 * @param mlen The length of the message to be hashed, measured in bytes.
 */
void sm3(u8 digest[32], const void *msg, u64 mlen);

#endif //SM_SM3_H

